***Variable preparation

*Political knowledge index (excludes question on Biafra as not in curriculum)
gen knowledge_index = Currentpresident_correct +Firstpresident_correct +Currentgovernor_correct +Votingage_correct +Lawmaking_correct +Independence_correct +Office_correct +Party_correct
summ knowledge_index
egen knowledge_indexS = std(knowledge_index)

*socio-economic background
factor Father_education Mother_education Private_school Food Books Computer_Ipad, pcf
predict SES
pwcorr SES Father_education Mother_education Private_school Food Books Computer_Ipad

*parental engagement
factor Political_interest_parents Hear_politics_home Talk_politics_home Parent_partypref , pcf
predict parent_engagement
pwcorr parent_engagement Political_interest_parents Hear_politics_home Talk_politics_home Parent_partypref

*political engagement
factor Pol_interest_rescaled Talk_politics_friends Television Radio Newspaper Internet, pcf
predict pol_engagement
pwcorr pol_engagement Pol_interest_rescaled Talk_politics_friends Television Radio Newspaper Internet

*democratic values
vreverse Power_in_one_person, gen(Power_in_one_person_rv)
vreverse Jobs_to_family, gen(Jobs_to_family_rv)
vreverse Pay_vote, gen(Pay_vote_rv)
vreverse Ethnic_vote, gen(Ethnic_vote_rv)

factor Power_in_one_person_rv Jobs_to_family_rv Pay_vote_rv Ethnic_vote_rv, pcf
predict democratic_values
pwcorr democratic_values Power_in_one_person_rv Jobs_to_family_rv Pay_vote_rv Ethnic_vote_rv

*institutional trust
vreverse Trust_President, gen(Trust_President_real)
vreverse Trust_Lagos_Government, gen(Trust_Lagos_Government_real)
vreverse Trust_Army, gen(Trust_Army_real)
vreverse Trust_Police, gen(Trust_Police_real)
vreverse Trust_parties, gen(Trust_parties_real)
factor Trust_President_real Trust_Lagos_Government_real Trust_Army_real Trust_Police_real Trust_parties_real, pcf
predict instit_trust
pwcorr instit_trust Trust_President Trust_Lagos_Government Trust_Army Trust_Police Trust_parties

* descriptive statistics
summ Vote_rescaled Party_member_rescaled Campaign_rescaled Protest_rescaled Contact_govt_rescaled
pwcorr Vote_rescaled Party_member_rescaled Protest_rescaled Campaign_rescaled Contact_govt_rescaled

summ knowledge_indexS pol_engagement democratic_values instit_trust

summ SES parent_engagement

tab Youth_group, gen(youthgr)
summ youthgr1 youthgr2 youthgr3
summ Age Female Urban
tab Yoruba
tab Igbo
tab Other_Ethnicity
summ Yoruba Igbo Other_Ethnicity

*bivariate testing
xtmixed  parent_engagement SES || School_ID:, variance
xtmixed  knowledge_indexS SES || School_ID:, variance
xtmixed  pol_engagement SES  || School_ID:, variance
xtmixed  democratic_values SES || School_ID:, variance
xtmixed  instit_trust SES  || School_ID:, variance
xtmixed  Vote_rescale SES || School_ID:, variance
xtmixed  Party_member_rescaled  SES || School_ID:, variance
xtmixed  Campaign_rescaled SES  || School_ID:, variance
xtmixed  Contact_govt_rescaled SES || School_ID:, variance
xtmixed  Protest_rescaled SES || School_ID:, variance

xtmixed  knowledge_indexS parent_engagement  || School_ID:, variance
xtmixed  pol_engagement  parent_engagement || School_ID:, variance
xtmixed  democratic_values  parent_engagement|| School_ID:, variance
xtmixed  instit_trust parent_engagement  || School_ID:, variance
xtmixed  Vote_rescale parent_engagement || School_ID:, variance
xtmixed  Party_member_rescaled  parent_engagement || School_ID:, variance
xtmixed Campaign_rescaled parent_engagement  || School_ID:, variance
xtmixed Contact_govt_rescaled parent_engagement || School_ID:, variance
xtmixed Protest_rescaled parent_engagement || School_ID:, variance

xtmixed pol_engagement knowledge_indexS  || School_ID:, variance
xtmixed democratic_values knowledge_indexS || School_ID:, variance
xtmixed instit_trust knowledge_indexS || School_ID:, variance
xtmixed Vote_rescale knowledge_indexS || School_ID:, variance
xtmixed Party_member_rescaled  knowledge_indexS || School_ID:, variance
xtmixed Campaign_rescaled knowledge_indexS  || School_ID:, variance
xtmixed Contact_govt_rescaled knowledge_indexS || School_ID:, variance
xtmixed Protest_rescaled knowledge_indexS || School_ID:, variance

xtmixed  democratic_values pol_engagement || School_ID:, variance
xtmixed  instit_trust pol_engagement || School_ID:, variance
xtmixed  Vote_rescale pol_engagement || School_ID:, variance
xtmixed  Party_member_rescaled  pol_engagement || School_ID:, variance
xtmixed  Campaign_rescaled pol_engagement   || School_ID:, variance
xtmixed  Contact_govt_rescaled pol_engagement || School_ID:, variance
xtmixed  Protest_rescaled pol_engagement || School_ID:, variance

xtmixed instit_trust democratic_values || School_ID:, variance
xtmixed Vote_rescale democratic_values || School_ID:, variance
xtmixed Party_member_rescaled democratic_values  || School_ID:, variance
xtmixed Campaign_rescaled  democratic_values || School_ID:, variance
xtmixed Contact_govt_rescaled democratic_values || School_ID:, variance
xtmixed Protest_rescaled democratic_values || School_ID:, variance

xtmixed Vote_rescale instit_trust || School_ID:, variance
xtmixed Party_member_rescaled  instit_trust || School_ID:, variance
xtmixed Campaign_rescaled instit_trust  || School_ID:, variance
xtmixed Contact_govt_rescaled instit_trust || School_ID:, variance
xtmixed Protest_rescaled instit_trust || School_ID:, variance


*multiple regressions
xtmixed  parent_engagement SES Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  knowledge_indexS SES Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  pol_engagement SES Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  democratic_values SES Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  instit_trust SES Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  Vote_rescale SES Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Party_member_rescaled  SES Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Campaign_rescaled SES Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  Contact_govt_rescaled SES Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Protest_rescaled SES Age Female Urban Igbo Other_Ethnicity || School_ID:, variance

xtmixed  knowledge_indexS parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  pol_engagement  parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  democratic_values  parent_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  instit_trust parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  Vote_rescale parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed  Party_member_rescaled  parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Campaign_rescaled parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Contact_govt_rescaled parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Protest_rescaled parent_engagement Age Female Urban Igbo Other_Ethnicity || School_ID:, variance

xtmixed pol_engagement knowledge_indexS Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed democratic_values knowledge_indexS Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed instit_trust knowledge_indexS Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Vote_rescale knowledge_indexS Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Party_member_rescaled  knowledge_indexS Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Campaign_rescaled knowledge_indexS  Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Contact_govt_rescaled knowledge_indexS Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Protest_rescaled knowledge_indexS Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance

xtmixed  democratic_values pol_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  instit_trust pol_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Vote_rescale pol_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Party_member_rescaled  pol_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Campaign_rescaled pol_engagement Age Female Urban Igbo Other_Ethnicity  || School_ID:, variance
xtmixed  Contact_govt_rescaled pol_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed  Protest_rescaled pol_engagement Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance

xtmixed  instit_trust democratic_values Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Vote_rescale democratic_values Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Party_member_rescaled democratic_values Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Campaign_rescaled  democratic_values Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Contact_govt_rescaled democratic_values Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Protest_rescaled democratic_values Age Female Urban Igbo Other_Ethnicity || School_ID:, variance

xtmixed  Vote_rescale instit_trust Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Party_member_rescaled  instit_trust Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance
xtmixed Campaign_rescaled instit_trust Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Contact_govt_rescaled instit_trust Age Female Urban Igbo Other_Ethnicity || School_ID:, variance
xtmixed Protest_rescaled instit_trust Age Female Urban Igbo Other_Ethnicity|| School_ID:, variance

*determinants of participation

xtmixed Vote_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity  || School_ID:, variance
estat icc
predict predictions
predict residuals, residuals
mltrsq 
// assumptions + effect size
reg Vote_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
estat esize
vif
predict cooksd, cooksd
summ cooksd
histogram residuals, frequency normal // normality
scatter residuals predictions // linearity + homoskedasticity
scatter residuals knowledge_index // linearity main continuous predictor
scatter residuals democratic_values // linearity main continuous predictor
scatter residuals pol_engagement // linearity main continuous predictor
scatter residuals instit_trust // linearity main continuous predictor
scatter residuals SES // linearity main continuous predictor
scatter residuals parent_engagement // linearity main continuous predictor
summ residuals
drop residuals predictions cooksd

xtmixed Party_member_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity  || School_ID:, variance
estat icc
predict predictions
predict residuals, residuals
mltrsq 
// assumptions + effect size
reg Party_member_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity 
estat esize
vif
predict cooksd, cooksd
summ cooksd
histogram residuals, frequency normal // normality
scatter residuals predictions // linearity + homoskedasticity
scatter residuals knowledge_index // linearity main continuous predictor
scatter residuals democratic_values // linearity main continuous predictor
scatter residuals instit_trust // linearity main continuous predictor
scatter residuals pol_engagement // linearity main continuous predictor
scatter residuals SES // linearity main continuous predictor
scatter residuals parent_engagement // linearity main continuous predictor
summ residuals
drop residuals predictions cooksd

xtmixed Campaign_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity  || School_ID:, variance
estat icc
predict predictions
predict residuals, residuals
mltrsq 
// assumptions + effect size
reg Campaign_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity 
estat esize
vif
predict cooksd, cooksd
summ cooksd
histogram residuals, frequency normal // normality
scatter residuals predictions // linearity + homoskedasticity
scatter residuals knowledge_index // linearity main continuous predictor
scatter residuals democratic_values // linearity main continuous predictor
scatter residuals instit_trust // linearity main continuous predictor
scatter residuals pol_engagement // linearity main continuous predictor
scatter residuals SES // linearity main continuous predictor
scatter residuals parent_engagement // linearity main continuous predictor
summ residuals
drop residuals predictions cooksd

xtmixed Contact_govt_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity  || School_ID:, variance
estat icc
predict predictions
predict residuals, residuals
mltrsq 
// assumptions + effect size
reg Contact_govt_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
estat esize
vif
predict cooksd, cooksd
summ cooksd
histogram residuals, frequency normal // normality
scatter residuals predictions // linearity + homoskedasticity
scatter residuals knowledge_index // linearity main continuous predictor
scatter residuals democratic_values // linearity main continuous predictor
scatter residuals instit_trust // linearity main continuous predictor
scatter residuals pol_engagement // linearity main continuous predictor
scatter residuals SES // linearity main continuous predictor
scatter residuals parent_engagement // linearity main continuous predictor
summ residuals
drop residuals predictions cooksd

xtmixed Protest_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust  SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity  || School_ID:, variance
estat icc
predict predictions
predict residuals, residuals
mltrsq 
// assumptions + effect size
reg Protest_rescaled knowledge_indexS  democratic_values pol_engagement instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
estat esize
vif
predict cooksd, cooksd
summ cooksd
histogram residuals, frequency normal // normality
scatter residuals predictions // linearity + homoskedasticity
scatter residuals knowledge_index // linearity main continuous predictor
scatter residuals democratic_values // linearity main continuous predictor
scatter residuals instit_trust // linearity main continuous predictor
scatter residuals pol_engagement // linearity main continuous predictor
scatter residuals SES // linearity main continuous predictor
scatter residuals parent_engagement // linearity main continuous predictor
summ residuals
drop residuals predictions cooksd

//binary
xtset School_ID
xtlogit Vote_dummy knowledge_indexS  democratic_values pol_engagement instit_trust  SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
xtlogit Party_dummy knowledge_indexS  democratic_values pol_engagement instit_trust  SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
xtlogit Campaign_dummy knowledge_indexS  democratic_values pol_engagement instit_trust  SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
xtlogit Contact_dummy knowledge_indexS  democratic_values pol_engagement instit_trust  SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
xtlogit Protest_dummy knowledge_indexS  democratic_values pol_engagement instit_trust  SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity

* LCA

// linear with fractional regression
gsem (Vote_rescaled Party_member_rescaled Protest_rescaled Campaign_rescaled Contact_govt_rescaled <-  _cons), lclass(C 3, base(3)) startvalues(randompr, draws(100) seed(15) difficult) emopts(iterate(30) difficult) vce(cluster School_ID)
estimates store threeclass
estat lcgof
estat lcprob
estat lcmean
predict pr*, classposteriorpr


gsem (Vote_rescaled Party_member_rescaled Protest_rescaled Campaign_rescaled Contact_govt_rescaled <-  _cons) , lclass(C 4) startvalues(randompr, draws(100) seed(15) difficult) emopts(iterate(30) difficult) vce(cluster School_ID)
estat lcgof
estat lcprob
estat lcmean
estimates store fourclass

estimates stats threeclass fourclass

fmlogit pr3 pr2 pr1 , etavar(knowledge_indexS pol_engagement democratic_values instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity) cluster(School_ID) 
margins, at(knowledge_index = (-5 1)) predict(outcome(pr3))
margins, at(knowledge_index = (-5 1)) predict(outcome(pr2))
margins, at(knowledge_index = (-5 1)) predict(outcome(pr1))
margins, at(pol_engagement = (-2 2)) predict(outcome(pr3))
margins, at(pol_engagement = (-2 2)) predict(outcome(pr2))
margins, at(pol_engagement = (-2 2)) predict(outcome(pr1))
margins, at(democratic_values = (-3 1.7)) predict(outcome(pr3))
margins, at(democratic_values = (-3 1.7)) predict(outcome(pr2))
margins, at(democratic_values = (-3 1.7)) predict(outcome(pr1))
margins, at(instit_trust = (-1.2 3)) predict(outcome(pr3))
margins, at(instit_trust = (-1.2 3)) predict(outcome(pr2))
margins, at(instit_trust = (-1.2 3)) predict(outcome(pr1))
margins, at(SES = (-3.4 2.4)) predict(outcome(pr3))
margins, at(SES = (-3.4 2.4)) predict(outcome(pr2))
margins, at(SES = (-3.4 2.4)) predict(outcome(pr1))
margins, at(parent_engagement = (-2.4 1.6)) predict(outcome(pr3))
margins, at(parent_engagement = (-2.4 1.6)) predict(outcome(pr2))
margins, at(parent_engagement = (-2.4 1.6)) predict(outcome(pr1))
margins, at(youthgr2 = (0 1)) predict(outcome(pr3))
margins, at(youthgr2 = (0 1)) predict(outcome(pr2))
margins, at(youthgr2 = (0 1)) predict(outcome(pr1))
margins, at(youthgr1 = (0 1)) predict(outcome(pr3))
margins, at(youthgr1 = (0 1)) predict(outcome(pr2))
margins, at(youthgr1 = (0 1)) predict(outcome(pr1))
margins, at(Age = (12 23)) predict(outcome(pr3))
margins, at(Age = (12 23)) predict(outcome(pr2))
margins, at(Age = (12 23)) predict(outcome(pr1))
margins, at(Female = (0 1)) predict(outcome(pr3))
margins, at(Female = (0 1)) predict(outcome(pr2))
margins, at(Female = (0 1)) predict(outcome(pr1))
margins, at(Urban = (0 1)) predict(outcome(pr3))
margins, at(Urban = (0 1)) predict(outcome(pr2))
margins, at(Urban = (0 1)) predict(outcome(pr1))
margins, at(Igbo = (0 1)) predict(outcome(pr3))
margins, at(Igbo = (0 1)) predict(outcome(pr2))
margins, at(Igbo = (0 1)) predict(outcome(pr1))
margins, at(Other_Ethnicity = (0 1)) predict(outcome(pr3))
margins, at(Other_Ethnicity = (0 1)) predict(outcome(pr2))
margins, at(Other_Ethnicity = (0 1)) predict(outcome(pr1))


fmlogit pr1 pr2 pr3 , etavar(knowledge_indexS pol_engagement democratic_values instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity) cluster(School_ID) 

*assumptions
gen membership = 1
replace membership =2 if pr2 > 0.5
replace membership =3 if pr3 > 0.5

tab membership, gen(member)
logit member2 knowledge_index pol_engagement democratic_values instit_trust SES parent_engagement youthgr2 youthgr1 Age Female Urban Igbo Other_Ethnicity
linktest
predict dbeta, dbeta
scatter dbeta FINAL_ID
predict residuals, residuals
summ residuals
predict logodds, xb
scatter logodds knowledge_index
scatter logodds pol_engagement
scatter logodds democratic_values
scatter logodds instit_trust
scatter logodds SES
scatter logodds parent_engagement
drop logodds
drop residuals
drop dbeta
